package com.l2jserver.gameserver.communitybbs.Manager;

import java.util.StringTokenizer;

import com.l2jserver.gameserver.cache.HtmCache;
import com.l2jserver.gameserver.datatables.MultiSell;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.zone.ZoneId;

public class ShopBBSManager extends BaseBBSManager
{
	
	@Override
	public void parsecmd(final String command, L2PcInstance activeChar)
	{
		String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/CommunityBoard/shop.htm");
		separateAndSend(content, activeChar);
		
		if (command.startsWith("_bbsmultisell;"))
		{
			if (!activeChar.isInsideZone(ZoneId.TOWN))
			{
				activeChar.sendMessage("You can shop only in towns");
				return;
			}
			StringTokenizer st = new StringTokenizer(command, ";");
			st.nextToken();
			TopBBSManager.getInstance().parsecmd("_bbslink;" + st.nextToken(), activeChar);
			MultiSell.getInstance().separateAndSend(Integer.parseInt(st.nextToken()), activeChar, null, false);
		}
		if (command.startsWith("_bbsscripts;"))
		{
			if (!activeChar.isInsideZone(ZoneId.TOWN))
			{
				activeChar.sendMessage("You can shop only in towns");
				return;
			}
			StringTokenizer st = new StringTokenizer(command, ";");
			st.nextToken();
			TopBBSManager.getInstance().parsecmd("_bbslink;" + st.nextToken(), activeChar);
			
			String com = st.nextToken();
			String[] word = com.split("\\s+");
			String[] args = com.substring(word[0].length()).trim().split("\\s+");
			String[] path = word[0].split(":");
			if (path.length != 2)
			{
				System.out.println("Bad Script bypass!");
				return;
			}
			
			if (word.length == 1)
			{
				activeChar.runScript(path[0], path[1]);
			}
			else
			{
				activeChar.runScript(path[0], path[1], args);
			}
		}
	}
	
	public static ShopBBSManager getInstance()
	{
		return SingletonHolder._instance;
	}
	
	private static class SingletonHolder
	{
		protected static final ShopBBSManager _instance = new ShopBBSManager();
	}
	
	@Override
	public void parsewrite(String s, String s1, String s2, String s3, String s4, L2PcInstance l2pcinstance)
	{
	}
}